package com.fingard.dsp.bank.directbank.hxb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;

import java.text.MessageFormat;
import java.util.Calendar;

public class HXB01Transaction extends HXB01Base implements ITransaction {
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();

        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            boolean qry7516Today = getBankConfig().getValueAsBool("qry7516Today");
            tmpCalendarTo.setTime(p_req7516.getDateTo());
            if (qry7516Today) {
                tmpCalendarTo.add(Calendar.DAY_OF_MONTH, -1);
            }

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }
                String sendInfo = MessageFormat.format("{0}#{1}#{2}#{3}#@@@@", "xhj0009", p_req7516.actNum,
                        Format.Date8Format.format(tmpCalendarFrom.getTime()), Format.Date8Format.format(tmpClndrStepTo.getTime()));

                String[] tmpStrRet = sendToBank(sendInfo);

                //tmpStrRet[0] = "";
                //tmpStrRet[1] = FileHelper.readFileText("D:\\Workspaces\\FilesForDevelop\\BankPort\\HXB01\\DSPNET-85\\7516_20161104104409_b6510468-9b6f-4681-8015-58c9b882f524 - 副本.txt", "gbk");

                if (tmpStrRet[0].length() == 0) {
                    String[] resultArr = tmpStrRet[1].split("#");
                    if (resultArr[0].equals("000000")) {
                        //tmpRespCode = "0";
                        int rdCount = Integer.parseInt(resultArr[2]);//获取循环条数
                        String actNumBk = resultArr[1].trim();//账号--银行返回
                        if (rdCount > 0) {
                            String recordText = tmpStrRet[1].substring(resultArr[0].length() + 1 + resultArr[1].length() + 1 + resultArr[2].length() + 1);
                            resultArr = null;
                            resultArr = recordText.split("\\|#");
                            int cmpCount = 0;
                            for (int i = 0; i < resultArr.length; i++) {
//0交易日期|1明细序号|2币种|3会计流水|4借贷标记|5交易金额|6对方账号|7对方账户名|8对方开户行|9账户余额|10摘要|11附言-渠道|12凭证种类-此字段不显示或显示字符|13备用域1[备注：]|14交易时间|
                                String[] recordArr = resultArr[i].split("\\|");
                                if (recordArr.length >= 15) {
                                    cmpCount++;
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    tmpRetDetail.actNum = actNumBk;
                                    tmpRetDetail.tranDate = DateHelper.convertD8toD10(recordArr[0]);
                                    tmpRetDetail.seqId = recordArr[1];
                                    if (StringHelper.hasAnyChar(recordArr[2])) {
                                        tmpRetDetail.cur = convertCurToATS(recordArr[2]);
                                    } else {
                                        tmpRetDetail.cur = p_req7516.actCur;
                                    }
                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    if (recordArr[14].trim().length() > 0) {
                                        tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + recordArr[14];
                                    } else {
                                        tmpRetDetail.tranTime = tmpRetDetail.tranDate;
                                    }
                                    if (recordArr[4].equals("1") || recordArr[4].equals("2")) {
                                        tmpRetDetail.cdFlag = recordArr[4];
                                    } else {
                                        tmpRespCode = "-1";
                                        tmpRespInfo = "借贷标记" + recordArr[4] + "不符合预期";
                                        break;
                                    }
                                    if (recordArr[5].trim().length() > 0)//交易金额
                                    {
                                        tmpRetDetail.tranAmt = recordArr[5].trim().replace("-", "");
                                        if (tmpRetDetail.cdFlag.equals("1") && recordArr[5].indexOf("-") >= 0) {
                                            tmpRetDetail.cdFlag = "2";
                                        } else if (tmpRetDetail.cdFlag.equals("2") && recordArr[5].indexOf("-") >= 0) {
                                            tmpRetDetail.cdFlag = "1";
                                        }
                                    }
                                    tmpRetDetail.recAccNum = recordArr[6];
                                    tmpRetDetail.recAccName = recordArr[7];
                                    tmpRetDetail.recAccBank = recordArr[8];
                                    tmpRetDetail.accBal = recordArr[9];
                                    tmpRetDetail.usage = recordArr[10];
                                    tmpRetDetail.payCode = recordArr[3];
                                    tmpRetDetail.abstr = pickupAbstr(recordArr[10], recordArr[11], recordArr[13]);
                                    tmpRetDetail.billType = recordArr[12];
                                    tmpRetDetail.comment = StringHelper.joinNotEmpty(";", recordArr[13], recordArr[11]);
                                    //ATSES-24144 dsp华夏银行电子回单补充任务规则
                                    tmpRetDetail.busRef = DateHelper.convertD10toD8(tmpRetDetail.tranDate) + tmpRetDetail.actNum + tmpRetDetail.recAccNum;
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                            if (tmpRespCode.equals("0") && rdCount != cmpCount) {
                                tmpRespCode = "-1";
                                tmpRespInfo = "预期总笔数" + rdCount + "与实际读取总笔数" + cmpCount + "不一致";
                            }
                        } else {
                            tmpRespInfo = tmpStrRet[1];
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = convertRespToBank(resultArr[0]);//获取银行返回信息"查询失败，银行返回码："+resultArr[0]
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 当日明细查询
     */
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
    	RetAtsTrans retAts = new RetAtsTrans(p_req7515.reqHeader);
        retAts.actNum = p_req7515.actNum;
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();

        try {
        	 String sendInfo = MessageFormat.format("{0}#{1}#0#@@@@", "xhj5006", p_req7515.actNum);

             String[] tmpStrRet = sendToBank(sendInfo);

             if (tmpStrRet[0].length() == 0) {
                 String[] resultArr = tmpStrRet[1].split("#");
                 if (resultArr[0].equals("000000")) {
                     int rdCount = Integer.parseInt(resultArr[2]);//获取循环条数
                     String actNumBk = resultArr[1].trim();//账号--银行返回
                     if (rdCount > 0) {
                         String recordText = tmpStrRet[1].substring(resultArr[0].length() + 1 + resultArr[1].length() + 1 + resultArr[2].length() + 1);
                         resultArr = null;
                         resultArr = recordText.split("\\|#");
                         int cmpCount = 0;
                         for (int i = 0; i < resultArr.length; i++) {
//0交易日期|1明细序号|2币种|3会计流水|4借贷标记|5交易金额|6对方账号|7对方账户名|8对方开户行|9账户余额|10摘要|11附言-渠道|12凭证种类-此字段不显示或显示字符|13备用域1[备注：]|14交易时间|

//0交易日期|1交易时间(xx:xx:xx)|2币种|3会计流水|4借贷标记|5交易金额|6对方账号|7对方账户名|8对方开户行|9账户余额|10摘要|11附言|12凭证种类|13企业端流水号|14备注1|15备注2|16备注3|
                        	 String[] recordArr = resultArr[i].split("\\|");
                             if (recordArr.length >= 15) {
                                 cmpCount++;
                                 RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                 tmpRetDetail.actNum = actNumBk;
                                 tmpRetDetail.tranDate = DateHelper.convertD8toD10(recordArr[0]);
                                 if (StringHelper.hasAnyChar(recordArr[2])) {
                                     tmpRetDetail.cur = convertCurToATS(recordArr[2]);
                                 } else {
                                     tmpRetDetail.cur = p_req7515.actCur;
                                 }
                                 tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                 if (recordArr[1].trim().length() > 0) {
                                     tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + recordArr[1];
                                 } else {
                                     tmpRetDetail.tranTime = tmpRetDetail.tranDate;
                                 }
                                 if (recordArr[4].equals("1") || recordArr[4].equals("2")) {
                                     tmpRetDetail.cdFlag = recordArr[4];
                                 } else {
                                     tmpRespCode = "-1";
                                     tmpRespInfo = "借贷标记" + recordArr[4] + "不符合预期";
                                     break;
                                 }
                                 if (recordArr[5].trim().length() > 0)//交易金额
                                 {
                                     tmpRetDetail.tranAmt = recordArr[5].trim().replace("-", "");
                                     if (tmpRetDetail.cdFlag.equals("1") && recordArr[5].indexOf("-") >= 0) {
                                         tmpRetDetail.cdFlag = "2";
                                     } else if (tmpRetDetail.cdFlag.equals("2") && recordArr[5].indexOf("-") >= 0) {
                                         tmpRetDetail.cdFlag = "1";
                                     }
                                 }
                                 tmpRetDetail.recAccNum = recordArr[6];
                                 tmpRetDetail.recAccName = recordArr[7];
                                 tmpRetDetail.recAccBank = recordArr[8];
                                 tmpRetDetail.accBal = recordArr[9];
                                 tmpRetDetail.usage = recordArr[11];
                                 tmpRetDetail.payCode = recordArr[3];
                                 tmpRetDetail.abstr = pickupAbstr(recordArr[10], recordArr[11], recordArr[14]);
                                 tmpRetDetail.billType = recordArr[12];
                                 tmpRetDetail.comment = StringHelper.joinNotEmpty(";", recordArr[10], recordArr[14]);
                                 retAts.writeRetDetail(tmpRetDetail);
                             }
                         }
                         if ("0".equals(tmpRespCode) && rdCount != cmpCount) {
                             tmpRespCode = "-1";
                             tmpRespInfo = "预期总笔数" + rdCount + "与实际读取总笔数" + cmpCount + "不一致";
                         }
                     } else {
                         tmpRespInfo = tmpStrRet[1];
                     }
                 } else {
                     tmpRespCode = "-1";
                     tmpRespInfo = convertRespToBank(resultArr[0]);//获取银行返回信息"查询失败，银行返回码："+resultArr[0]
                 }
             } else {
                 tmpRespCode = "-1";
                 tmpRespInfo = tmpStrRet[1];
             }
        } catch (Exception ex) {
        	ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }

        retAts.writeEnd();
        return retAts;
    }
}
